#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

# 依赖库：PyMySQL

import pymysql
import utils.db
import utils.def_parse

db_config = utils.def_parse.def2map("../mysql_config.def")

db = pymysql.connect(
  host=db_config['HOST']            # 数据库主机地址
  ,user=db_config['USER']           # 数据库用户名
  ,password=db_config['PASSWORD']   # 数据库密码
  ,database=db_config['DATABASE']   # 数据库名称
  ,port=int(db_config['PORT'])           # 数据库端口, 默认为3306
)

tables = [
  utils.db.get_sql("./sql/create_tables/ModuleDependency.sql")  # ModuleDependency
  ,utils.db.get_sql("./sql/create_tables/SymbolDefinition.sql") # SymbolDefinition
  ,utils.db.get_sql("./sql/create_tables/SymbolDependencyBetweenModules.sql")   # SymbolDependencyBetweenModules
  ,utils.db.get_sql("./sql/create_tables/SymbolDependencyBetweenModulesCompressed.sql") # SymbolDependencyBetweenModulesCompressed
  ,utils.db.get_sql("./sql/create_tables/SymbolUse.sql")  # SymbolUse
  ,utils.db.get_sql("./sql/create_tables/SymbolUseCompressed.sql")  # SymbolUseCompressed
  ,utils.db.get_sql("./sql/create_tables/TypeMetadata.sql") # TypeMetadata
  ,utils.db.get_sql("./sql/create_tables/VirtualCall.sql")  # VirtualCall
  ,utils.db.get_sql("./sql/create_tables/Vtables.sql")  # Vtables
]
cursor = db.cursor()

try:
  for cmds in tables:
    for cmd in cmds:
      cursor.execute(cmd)
except Exception as e:
  print(e)
else:
  db.commit()
  # 关闭数据库连接
  db.close()
  print("Tables are created successfully.")